package com.dingreading.cloud.soms.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.FileUtil;
import com.dingreading.cloud.common.util.LocalFileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/system/localUpload")
@Api(tags = "本地-上传文件")
public class LocalUploadController {


    @ApiOperation(value = "付款截图上传（递归设置新建文件夹的所有者和组）")
    @PostMapping(value = "/v2/payimgs", produces = "application/json; charset=UTF-8")
    public R<Object> payimgs2(
            @ApiParam(value = "图片文件") @RequestPart(value = "file") MultipartFile file
    ) throws Exception {
        if (null == file) return R.fail("图片不能为空");
        boolean m = FileUtil.checkFileSize(file.getSize(), 10, "M");
        if (!m) return R.fail("图片大小不能超过10M");

        String fileName = LocalFileUtil.getFileName(file);
        if (StringUtils.isBlank(fileName)) return R.fail("获取图片名失败");

        if (!fileName.contains(".jpg") && !fileName.contains(".jpeg") && !fileName.contains(".png"))
            return R.fail("请上传png,jpg,jpeg格式的图片");

        String nowDateStr = DateUtil.getNowDateStr();
        String[] split = StringUtils.split(nowDateStr, "-");
        // linux文件路径
        String baseDir = LocalFileUtil.payImgBaseDir;
        String ymd = split[0] + "/" + split[1] + "/" + split[2] + "/";

        // 文件夹不存在则创建
        String dirPath = baseDir;
        List<String> newDirs = new ArrayList<>();
        File baseFile = new File(dirPath);
        if (!baseFile.exists()) {
            boolean mkdirs = baseFile.mkdirs();
            if (mkdirs)
                newDirs.add(dirPath);
        }
        for (int i = 0; i < split.length; i++) {
            dirPath = dirPath + split[i] + "/";
            File dir = new File(dirPath);
            if (!dir.exists()) {
                boolean mkdir = dir.mkdir();
                if (mkdir)
                    newDirs.add(dirPath);
            }
        }
        // 将要保存的文件加入更改权限的列表中
        String newFilePath = baseDir + ymd + fileName;
        newDirs.add(newFilePath);

        //保存照片到指定路径
        File newFile = new File(newFilePath);
        file.transferTo(newFile);

        // 如果有新建的文件夹，重新设置文件夹的所有者和组
        if (CollectionUtils.isNotEmpty(newDirs)) {
            String owner = "www";
            String group = "www";
            LocalFileUtil.changePermission(newDirs, owner, group);
        }

        return R.ok(ymd + fileName);
    }

}
